package com.taotao.manager.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.bean.ItemCatResult;
import com.taotao.manager.service.ItemCatService;

@Controller
@RequestMapping("api/item/cat")
public class ApiItemCatController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiItemCatController.class);
	
	@Autowired
	private ItemCatService itemCatService;
	
	private static final ObjectMapper MAPPER = new ObjectMapper(); 
	
	/**
	 * 对外提供接口服务，查询所有类目数据
	 * <p>Title: queryItemCatList</p>
	 * <p>Description: </p>
	 * @return
	 */
//	@RequestMapping(method = RequestMethod.GET)   ---手动支持jsonp
//	public ResponseEntity<String> queryItemCatList(@RequestParam(value="callback" ,required=false) String callback){
//		try {
//			ItemCatResult result = itemCatService.queryAllToTree();
//			String json = MAPPER.writeValueAsString(result);
//			if(StringUtils.isEmpty(callback)) {
//				return ResponseEntity.ok(json);
//			}
//			return ResponseEntity.ok(callback + "("+json+");");
//		} catch (Exception e) {
//			LOGGER.error("查询所有商品类目失败...",e);
//		}
//		
//		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//	}
	
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<ItemCatResult> queryItemCatList(@RequestParam(value="callback" ,required=false) String callback){
		try {
			ItemCatResult result = itemCatService.queryAllToTree();
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			LOGGER.error("查询所有商品类目失败...",e);
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
